<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="C++教程（49 数组-二维数组案例-考试成绩统计_哔哩哔哩_bilibili） AHdark Blog - Technology &amp; Social Life 这是B站上一个关于C++的教程">
<meta property="og:type" content="article">
<meta property="og:title" content="C++学习">
<meta property="og:url" content="https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/index.html">
<meta property="og:site_name" content="山野笨拙">
<meta property="og:description" content="C++教程（49 数组-二维数组案例-考试成绩统计_哔哩哔哩_bilibili） AHdark Blog - Technology &amp; Social Life 这是B站上一个关于C++的教程">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-03-21T16:00:00.000Z">
<meta property="article:modified_time" content="2023-03-29T11:26:12.579Z">
<meta property="article:author" content="man虎下山">
<meta property="article:tag" content="after_end">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="/develop/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/develop/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/develop/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>C++学习</title>
    <!-- async scripts -->
    <!-- Google Analytics -->


    <!-- Umami Analytics -->


    <!-- styles -->
    
<link rel="stylesheet" href="/develop/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fa-solid fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fa-solid fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fa-solid fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/develop/">Home</a></li><!--
     --><!--
       --><li><a href="/develop/about/">About</a></li><!--
     --><!--
       --><li><a href="/develop/archives/">Writing</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/probberechts">Projects</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="Previous post" href="/develop/2023/03/23/front_end/%E7%9F%A5%E4%B9%8E%EF%BC%9AJS%E9%9B%B6%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/"><i class="fa-solid fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="Next post" href="/develop/2023/01/29/front_end/%E5%89%8D%E7%AB%AF%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/"><i class="fa-solid fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="Back to top" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa-solid fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="Share post" href="#"><i class="fa-solid fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&text=C++学习"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&is_video=false&description=C++学习"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=C++学习&body=Check out this article: https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/"><i class="fa-solid fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&name=C++学习&description=&lt;p&gt;C++教程（&lt;a href=&#34;https://www.bilibili.com/video/BV1et411b73Z?p=49&amp;spm_id_from=pageDriver&amp;vd_source=6b7e87650266a74ff4624cb1cd120a53&#34;&gt;49 数组-二维数组案例-考试成绩统计_哔哩哔哩_bilibili&lt;/a&gt;）&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.ahdark.blog/&#34;&gt;AHdark Blog - Technology &amp;amp; Social Life&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;这是B站上一个关于C++的教程&lt;/p&gt;
&lt;p&gt;&lt;em&gt;"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&t=C++学习"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F"><span class="toc-number">1.</span> <span class="toc-text">1.冒泡排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%A9%BA%E6%8C%87%E9%92%88%E5%92%8C%E9%87%8E%E6%8C%87%E9%92%88"><span class="toc-number">2.</span> <span class="toc-text">2.空指针和野指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-const%E4%B8%8E%E6%8C%87%E9%92%88"><span class="toc-number">3.</span> <span class="toc-text">3.const与指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E6%95%B0%E7%BB%84-%E6%8C%87%E9%92%88-%E6%8E%92%E5%BA%8F"><span class="toc-number">4.</span> <span class="toc-text">4.数组+指针+排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%ADconst%E4%BD%BF%E7%94%A8"><span class="toc-number">5.</span> <span class="toc-text">5.结构体中const使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-%E7%BB%93%E6%9E%84%E4%BD%93%E5%B5%8C%E5%A5%97%E6%A1%88%E4%BE%8B"><span class="toc-number">6.</span> <span class="toc-text">6.结构体嵌套案例</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4 ">
        
        <article class="post h-entry" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle p-name" itemprop="name headline">
        C++学习
    </h1>



    <div class="meta">
      <span class="author p-author h-card" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span class="p-name" itemprop="name">man虎下山</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2023-03-21T16:00:00.000Z" class="dt-published" itemprop="datePublished">2023-03-22</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fa-solid fa-archive"></i>
        <a class="category-link" href="/develop/categories/after-end/">after_end</a>
    </div>


      
    <div class="article-tag">
        <i class="fa-solid fa-tag"></i>
        <a class="p-category" href="/develop/tags/after-end/" rel="tag">after_end</a>
    </div>


    </div>
  </header>
  

  <div class="content e-content" itemprop="articleBody">
    <p>C++教程（<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=49&spm_id_from=pageDriver&vd_source=6b7e87650266a74ff4624cb1cd120a53">49 数组-二维数组案例-考试成绩统计_哔哩哔哩_bilibili</a>）</p>
<p><a target="_blank" rel="noopener" href="https://www.ahdark.blog/">AHdark Blog - Technology &amp; Social Life</a></p>
<p>这是B站上一个关于C++的教程</p>
<p><em><span id="more"></span></em></p>
<h4 id="1-冒泡排序"><a href="#1-冒泡排序" class="headerlink" title="1.冒泡排序"></a>1.冒泡排序</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> arr[] = &#123; <span class="number">2</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">8</span>, <span class="number">9</span> &#125;;</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">    <span class="comment">//冒泡排序，排序的次数为数组的长度-1次</span></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>;i &lt; <span class="built_in">sizeof</span>(arr) / <span class="built_in">sizeof</span>(arr[<span class="number">0</span>]) - <span class="number">2</span>;i++)&#123;</span><br><span class="line">        <span class="comment">//每一次排序的交换次数为数组的长度-i-1次</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j = <span class="number">0</span>;j &lt; <span class="built_in">sizeof</span>(arr) / <span class="built_in">sizeof</span>(arr[<span class="number">0</span>]) - i - <span class="number">2</span>;j++)&#123;</span><br><span class="line">            <span class="keyword">if</span>(arr[j] &gt; arr[j+<span class="number">1</span>])&#123;</span><br><span class="line">                <span class="type">int</span> temp = arr[j];</span><br><span class="line">                arr[j] = arr[j+<span class="number">1</span>];</span><br><span class="line">                arr[j+<span class="number">1</span>] = temp;</span><br><span class="line">            &#125; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>;i &lt; <span class="number">9</span>;i++)&#123;</span><br><span class="line">        cout &lt;&lt; arr[i] &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-空指针和野指针"><a href="#2-空指针和野指针" class="headerlink" title="2.空指针和野指针"></a>2.空指针和野指针</h4><p>空指针（int *p &#x3D; NULL; *p &#x3D; 100;)： 是用来给指针设定初始的默认值为空的，但空指针不能直接进行使用来赋值，因为0~255是系统空间使用的，自己无法占用<br>野指针(int *p &#x3D; （int *)0x1100;)： 指没有申请这个内存空间但进行了使用，但无法被访问</p>
<p>上面两个指针都不是我们自己申请的空间，因此不要访问</p>
<h4 id="3-const与指针"><a href="#3-const与指针" class="headerlink" title="3.const与指针"></a>3.const与指针</h4><ol>
<li>const修饰指针  —- 常量指针（特点：指针的指向可以修改，但指针指向的值不可以修改）<ul>
<li>const int *p &#x3D; &amp;a; <ul>
<li>*p &#x3D; 20; 错误，指针指向的值不可以修改</li>
<li>p &#x3D; &amp;b; 正确，指针指向的地址可以修改</li>
</ul>
</li>
</ul>
</li>
<li>const修饰常量  —指针常量(特点：指针的指向不可以修改，但指针指向的值可以修改)<ul>
<li>int * const p &#x3D; 20;<ul>
<li>*p &#x3D; 20; 正确，指向的值可以改</li>
<li>p &#x3D; &amp;b; 错误，指针指向不可以改</li>
</ul>
</li>
</ul>
</li>
</ol>
<h4 id="4-数组-指针-排序"><a href="#4-数组-指针-排序" class="headerlink" title="4.数组+指针+排序"></a>4.数组+指针+排序</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="comment">//#include &quot;swap.h&quot;</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//从小到大冒泡排序</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swap</span><span class="params">(<span class="type">int</span> *arr, <span class="type">int</span> len)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; len - i - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (arr[j] &gt; arr[j + <span class="number">1</span>]) &#123;</span><br><span class="line">                <span class="type">int</span> temp = arr[j];</span><br><span class="line">                arr[j] = arr[j + <span class="number">1</span>];</span><br><span class="line">                arr[j + <span class="number">1</span>] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//输出数组</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">printArr</span><span class="params">(<span class="type">int</span> *arr, <span class="type">int</span> len)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;</span><br><span class="line">        cout &lt;&lt; arr[i] &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//2.数组+指针+排序</span></span><br><span class="line">    <span class="type">int</span> arr[] = &#123;<span class="number">5</span>,<span class="number">6</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">7</span>,<span class="number">10</span> &#125;;</span><br><span class="line">    <span class="type">int</span> len = <span class="keyword">sizeof</span> arr / <span class="keyword">sizeof</span> arr[<span class="number">0</span>];</span><br><span class="line">    <span class="built_in">swap</span>(arr,len);</span><br><span class="line">    <span class="built_in">printArr</span>(arr,len);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-结构体中const使用"><a href="#5-结构体中const使用" class="headerlink" title="5.结构体中const使用"></a>5.结构体中const使用</h4><p>const用在结构体指针上，是为了在调用函数传递结构体指针参数的时候让结构体指针不能被修改，保证内部的调用不会改变外部的值。同<strong>时调用函数时候的这个函数参数之所以要写成指针传递，是为了节省内存空间</strong>，如果是值传递要把原有的数据重新复制一份然后传递过去，而只是地址传递的话只需要声明地址所需的4 or 8个字节。</p>
<p>例子：**function(const struct *student stu)**，</p>
<h4 id="6-结构体嵌套案例"><a href="#6-结构体嵌套案例" class="headerlink" title="6.结构体嵌套案例"></a>6.结构体嵌套案例</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;string&gt;</span><br><span class="line">using namespace std;</span><br><span class="line"></span><br><span class="line">struct student&#123;</span><br><span class="line">    string name;</span><br><span class="line">    int age;</span><br><span class="line">    int score;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">struct teacher&#123;</span><br><span class="line">    string name;</span><br><span class="line">    struct student stu[<span class="number">5</span>];</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//给结构体赋初值</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">allocateInfo</span>(<span class="params">struct teacher t1[],int len</span>)&#123;</span><br><span class="line">    string nameInfo = <span class="string">&quot;ABCDE&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span>(int i = <span class="number">0</span>;i &lt; len;i++)&#123;</span><br><span class="line">        t1[i].<span class="property">name</span> = <span class="string">&quot;teacher&quot;</span>;</span><br><span class="line">        t1[i].<span class="property">name</span> += nameInfo[i];</span><br><span class="line">        int len2 = sizeof t1-&gt;stu / sizeof t1-&gt;stu[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span>(int j = <span class="number">0</span>;j &lt; len2;j++)&#123;</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">name</span> = <span class="string">&quot;student&quot;</span>;</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">name</span> += nameInfo[j];</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">age</span> = <span class="title function_">rand</span>() % <span class="number">18</span> + <span class="number">10</span>;</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">score</span> = <span class="title function_">rand</span>() % <span class="number">60</span> + <span class="number">40</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//输出结构体</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">printStruct</span>(<span class="params">struct teacher t1[],int len</span>)&#123;</span><br><span class="line">    <span class="keyword">for</span>(int i = <span class="number">0</span>;i &lt; len;i++)&#123;</span><br><span class="line">        int len2 = sizeof t1-&gt;stu / sizeof t1-&gt;stu[<span class="number">0</span>];</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;Teacher name:&quot;</span> &lt;&lt; t1[i].<span class="property">name</span> &lt;&lt; endl;</span><br><span class="line">        <span class="keyword">for</span>(int j = <span class="number">0</span>;j &lt; len2;j++)&#123;</span><br><span class="line">            cout &lt;&lt;</span><br><span class="line">            <span class="string">&quot;student is &quot;</span> &lt;&lt;</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">name</span> &lt;&lt; <span class="string">&quot;, age is &quot;</span> &lt;&lt;</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">age</span>  &lt;&lt; <span class="string">&quot;, score is &quot;</span> &lt;&lt;</span><br><span class="line">            t1[i].<span class="property">stu</span>[j].<span class="property">score</span>  &lt;&lt; endl;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125; </span><br><span class="line">int <span class="title function_">main</span>(<span class="params"></span>) &#123;</span><br><span class="line">    struct teacher t1[<span class="number">5</span>];</span><br><span class="line">    int len1 = sizeof t1 / sizeof t1[<span class="number">0</span>];</span><br><span class="line">    <span class="title function_">srand</span>((unsigned int)<span class="title function_">time</span>(<span class="variable constant_">NULL</span>));</span><br><span class="line">    <span class="title function_">allocateInfo</span>(t1,len1);</span><br><span class="line">    <span class="title function_">printStruct</span>(t1,len1);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
        
          <li><a href="/develop/">Home</a></li>
        
          <li><a href="/develop/about/">About</a></li>
        
          <li><a href="/develop/archives/">Writing</a></li>
        
          <li><a target="_blank" rel="noopener" href="http://github.com/probberechts">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F"><span class="toc-number">1.</span> <span class="toc-text">1.冒泡排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%A9%BA%E6%8C%87%E9%92%88%E5%92%8C%E9%87%8E%E6%8C%87%E9%92%88"><span class="toc-number">2.</span> <span class="toc-text">2.空指针和野指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-const%E4%B8%8E%E6%8C%87%E9%92%88"><span class="toc-number">3.</span> <span class="toc-text">3.const与指针</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E6%95%B0%E7%BB%84-%E6%8C%87%E9%92%88-%E6%8E%92%E5%BA%8F"><span class="toc-number">4.</span> <span class="toc-text">4.数组+指针+排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E7%BB%93%E6%9E%84%E4%BD%93%E4%B8%ADconst%E4%BD%BF%E7%94%A8"><span class="toc-number">5.</span> <span class="toc-text">5.结构体中const使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-%E7%BB%93%E6%9E%84%E4%BD%93%E5%B5%8C%E5%A5%97%E6%A1%88%E4%BE%8B"><span class="toc-number">6.</span> <span class="toc-text">6.结构体嵌套案例</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&text=C++学习"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&is_video=false&description=C++学习"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=C++学习&body=Check out this article: https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/"><i class="fa-solid fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&title=C++学习"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&name=C++学习&description=&lt;p&gt;C++教程（&lt;a href=&#34;https://www.bilibili.com/video/BV1et411b73Z?p=49&amp;spm_id_from=pageDriver&amp;vd_source=6b7e87650266a74ff4624cb1cd120a53&#34;&gt;49 数组-二维数组案例-考试成绩统计_哔哩哔哩_bilibili&lt;/a&gt;）&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.ahdark.blog/&#34;&gt;AHdark Blog - Technology &amp;amp; Social Life&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;这是B站上一个关于C++的教程&lt;/p&gt;
&lt;p&gt;&lt;em&gt;"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://ke-jiabiao.gitee.io/develop/2023/03/22/after_end/C++%E6%95%99%E7%A8%8B%EF%BC%8849%20%E6%95%B0%E7%BB%84-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A1%88%E4%BE%8B-%E8%80%83%E8%AF%95%E6%88%90%E7%BB%A9%E7%BB%9F%E8%AE%A1%E5%93%94%E5%93%A9%E5%93%94%E5%93%A9bilibili%EF%BC%89/&t=C++学习"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fa-solid fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fa-solid fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fa-solid fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fa-solid fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2016-2023
    man虎下山
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/develop/">Home</a></li><!--
     --><!--
       --><li><a href="/develop/about/">About</a></li><!--
     --><!--
       --><li><a href="/develop/archives/">Writing</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/probberechts">Projects</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script>
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="fa-regular fa-clone"></i>';
    btn += '</span>';
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/develop/js/main.js"></script>

<!-- search -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

</body>
</html>
